using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using Helper;

namespace Miner_Yedion
{
    public class CourseComparer
    {
        public string Name;

        public bool CourseComparer_Method(rootSubject r)
        {
            return (r.Name == Name);
        }
        public bool CourseComparer_Method(rootNotebook r)
        {
            return (r.Name == Name);
        }
    }

    public class Yedion_XML_API
    {
        public const string YEDION_FILENAME = "Yedion.xml";

        private static Yedion_XML_API instance;

        private string filenameField;
        private root MainRoot;

        public rootSubject[] Subjects;
        public rootNotebook[] Notebooks;

        private Logger logger = null;


        private Yedion_XML_API()
        {
            Array.Resize(ref Subjects, 0);
            Array.Resize(ref Notebooks, 0);


            MainRoot = new root();
            logger = Logger.Instance;
        }

        public static Yedion_XML_API Instance
        {
            get
            {
                if (instance == null)
                    instance = instance = new Yedion_XML_API();

                return instance;
            }
        }
        public string FileName
        {
            get
            {
                return this.filenameField;
            }
            set
            {
                this.filenameField = value;
            }
        }

        public void Save_To_Xml()
        {
            MainRoot.Grades = Subjects;
            MainRoot.Notebooks = Notebooks;

            StreamWriter SW = new StreamWriter(YEDION_FILENAME);
            XmlSerializer xSer = new XmlSerializer(typeof(root));
            xSer.Serialize(SW, MainRoot);
            SW.Close();

        }
        public void Load_From_Xml()
        {
            logger.Log_Method();
            logger.Step_In();

            if (!File.Exists(YEDION_FILENAME))
            {
                logger.WriteLn("FileName:" + YEDION_FILENAME + "Not Found...");
                logger.Step_Out();
                return;
            }

            StreamReader SR = new StreamReader(YEDION_FILENAME);
            XmlSerializer xSer = new XmlSerializer(typeof(root));
            MainRoot = (root)xSer.Deserialize(SR);
            SR.Close();

            Subjects = MainRoot.Grades;
            Notebooks = MainRoot.Notebooks;

            logger.Step_Out();
        }

        public void Add_Subject(rootSubject RS)
        {
            Array.Resize(ref Subjects, Subjects.Length + 1);
            Subjects.SetValue(RS, Subjects.Length - 1);
        }
        public void Add_Notebook(rootNotebook RN)
        {
            Array.Resize(ref Notebooks, Notebooks.Length + 1);
            Notebooks.SetValue(RN, Notebooks.Length - 1);

        }
        public void Add_Notebooks_To_Xml(rootNotebook[] List)
        {
            CourseComparer Cmp = new CourseComparer();
            foreach (rootNotebook rec in List)
            {
                Cmp.Name = rec.Name;
                rootNotebook r = Array.Find(Notebooks, Cmp.CourseComparer_Method);
                if (r == null)
                    Add_Notebook(rec);
            }
        }

        public void Add_Subjects_To_Xml(rootSubject[] List)
        {
            CourseComparer Cmp = new CourseComparer();
            foreach (rootSubject rec in List)
            {
                Cmp.Name = rec.Name;
                rootSubject  r = Array.Find(Subjects, Cmp.CourseComparer_Method);
                if (r == null)
                    Add_Subject(rec);
            }
        }

        public void Set_Subject_Check_Attribute(string name, Boolean value)
        {
            CourseComparer Cmp = new CourseComparer();
            Cmp.Name = name;
            rootSubject r = Array.Find(Subjects, Cmp.CourseComparer_Method);
            if (r != null)
                r.Checked = value;
        
        }
        public void Set_Notebook_Check_Attribute(string name, Boolean value)
        {
            CourseComparer Cmp = new CourseComparer();
            Cmp.Name = name;
            rootNotebook r = Array.Find(Notebooks, Cmp.CourseComparer_Method);
            if (r != null)
                r.Checked = value;

        }

        public rootSubject Find_Difference_Subject(rootSubject source)
        {
            CourseComparer Cmp = new CourseComparer();
            Cmp.Name = source.Name;
            rootSubject r = Array.Find(Subjects, Cmp.CourseComparer_Method);
            if (r == null)
                return null;

            if (r.Grade != source.Grade)
                r.Checked = true;

            return r;
        
        }

        public rootNotebook Find_Difference_Notebook(rootNotebook source)
        {
            CourseComparer Cmp = new CourseComparer();
            Cmp.Name = source.Name;
            rootNotebook r = Array.Find(Notebooks, Cmp.CourseComparer_Method);
            if (r == null)
                return null;

            if (r.Grade != source.Grade)
                r.Checked = true;

            return r;

        }
    }

}
